import { defineStore } from 'pinia';
import { reactive } from 'vue';
import { dictData } from '@/utlis/interface/UserAgreenmentText'
import { userInfoMy, riderLaction } from "@/utlis/interface/Wode"
interface useStore {
    userWorkStatus: number | string | null,
    userRealNameStatus: number | string | null,
    userAvatar: string | null,
    userNickName: string | null,
    userCompletedOrderCount: number | string | null,
    userMobile: number | string | null,
    userSatisfactionDegree: number | string | null,
    userTotalOrder: number | string | null,
    moduleShow: any
}
/**
 * @公用全局参数
 */
export const Public = defineStore('Public', () => {
    const Publics = reactive({
        Authorization: uni.getStorageSync('Authorization') //Token
    })
    return { Publics }
})

export const userStatusSet = defineStore('userStatusSet', () => {
    const myStatus = reactive({
        status: uni.getStorageSync('MYSTATUS'),
    })
    return { myStatus }

})
// useStore 可以是 useUser、useCart 之类的任何东西
// 第一个参数是应用程序中 store 的唯一 id
export const useStore = defineStore('user', () => {
    const infoData = reactive<useStore>({
        userWorkStatus: null,
        userRealNameStatus: null,
        userAvatar: null,
        userNickName: null,
        userCompletedOrderCount: null,
        userMobile: null,
        userSatisfactionDegree: null,
        userTotalOrder: null,
        moduleShow: false
    })
    const setStatus = (Proxy: any) => {
        return new Promise((resolve) => {
            userInfoMy('', Proxy).then((res: any) => {
                //console.log("res.data.realNameStatus = "+res.data.realNameStatus)
                infoData.userWorkStatus = res.data.workStatus
                infoData.userRealNameStatus = res.data.realNameStatus
                infoData.moduleShow = res.data.realNameStatus == 0
                infoData.userAvatar = res.data.avatar
                infoData.userNickName = res.data.nickName
                infoData.userCompletedOrderCount = res.data.completedOrderCount
                infoData.userMobile = res.data.mobile
                uni.setStorageSync('userMobile', res.data.mobile)
                infoData.userSatisfactionDegree = res.data.satisfactionDegree
                infoData.userTotalOrder = res.data.totalOrder
                resolve('')
            })

        })
    }

    return {
        setStatus,
        infoData
    }
})
export const riderLoaction = defineStore('riderLoaction', () => {
    const baseData = reactive<any>({
        timeValue: null,
    })
    // 根据字典去查询当前骑手更新位置时间间隔
    const setRiderTimes = (Type: number | string) => {
        return new Promise((resolve) => {
            dictData(`rider_is_there_order`, Proxy).then((res: any) => {
                if (res.code == 0) {
                    baseData.timeValue = res.data.itemList.rider_is_there_order[Type].value
                    uni.setStorageSync('timeValue', res.data.itemList.rider_is_there_order[Type].value)
                    resolve('')
                }
                // console.log(baseData.timeValue)
            })
        })
    }
    const updateRiderLoaction = (loaction: any) => {
        riderLaction(loaction, Proxy).then((res: any) => {

        })
    }
    return {
        updateRiderLoaction,
        setRiderTimes,
        baseData
    }


})


/**
 * @退出登录
 */
export const exitLogin = defineStore('exitLogin', () => {
    const exit = () => {
        uni.removeStorageSync('Authorization')
        uni.removeStorageSync('timeValue')
        uni.removeStorageSync('userMobile')
        uni.removeStorageSync('riderLongitude')
        uni.removeStorageSync('riderLatitude')
        uni.removeStorageSync('startingLatitude')
        uni.removeStorageSync('startingLongitude')
        uni.removeStorageSync('terminalLatitude')
        uni.removeStorageSync('terminalLongitude')
        setTimeout(() => {

            uni.redirectTo({
                url: '/pages/Login/Login',
            })
        }, 1500)
    }
    return { exit }
})
export const kefuCallPhone = defineStore('kefuCallPhone', () => {
    const phonelist = reactive<any>({
        list: []
    })

    const phoneNumber = (data: any) => {
        dictData(`dict_rider_csphone`, Proxy).then((res: any) => {
            phonelist.list = res.data.itemMap.dict_rider_csphone.value
        })
    }
    return {
        phonelist, phoneNumber

    }

})
/**
 * 新订单数量
 */
export const NewOrder = defineStore('newOrder', () => {
    const NewOrders = reactive({
        order0: 0,
        order1: 0
    })
    return { NewOrders }
})

/**
 * 定位缓存
 */
export const riderLocationCache = defineStore('riderLocationCache', () => {
    const locationCacheObj = reactive({
        lon: 0,
        lat: 0
    })
    return { locationCacheObj }
})

// /**
//  * @清除缓存
//  */
// export const removeStor = defineStore('removeStor', () => {
//     uni.clearStorageSync()
// })
